//
//  YHNetwork.m
//  DirectorActor
//
//  Created by MinwenYi on 14-9-30.
//  Copyright (c) 2014年 MinwenYi. All rights reserved.
//

#import "YHNetwork.h"
#import "AFURLConnectionOperation.h"
#import "AFHTTPRequestOperationManager.h"
@implementation YHNetwork

/**
 *  Description
 *
 *  @param type    请求类型
 *  @param params  参数
 *  @param Api     接口api
 *  @param success 成功时调用
 *  @param failure 失败时调用
 */


+ (void)getInformationWithReguestType:(RequestType)type WithParameters:(NSMutableDictionary *)params  WithApi:(NSString *)Api WhenSuccess:(success)success WhenFailure:(failure)failure

{
    
    
     [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    //主机地址和参数
   //115.29.141.60:8080/ameiliServer/UserService?operatetype=getmessage
//<<<<<<< HEAD
//    NSString *url = @"http://115.29.141.60:8080/ameiliServer/";
    NSString *url = @"http://cent.dsgrd.com/";
    

//>>>>>>> b24cf1450d7f89474408e3ba24c65e0c00c6b069
    //得到最后地址
    NSString *finalUrl = [url stringByAppendingString:Api];
    NSLog(@"finalUrl=======%@",finalUrl);
   //get方式还是Post方式
    NSString *httpMethod = nil;
    
//    BOOL cani = YES;
//    
//    cani = [self www];
//    
//    if (!cani) {
//        
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOWWW" object:nil];
//        
//        
//    
//    }
    switch (type)
    {
        case RequestTypePost:
            httpMethod = @"POST";
            break;
        case RequestTypeGet:
            httpMethod = @"GET";
            break;
        default:
            break;
    }
    
    
    if ([httpMethod isEqualToString:@"POST"])
    {
        // POST方式请求数据
        AFHTTPRequestOperationManager *peration = [AFHTTPRequestOperationManager manager];
        
        
    
        
        
//        - (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field; 
        //        peration.responseSerializer = [AFJSONResponseSerializer serializer];
        peration.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/plain",@"text/html", nil];
        peration.responseSerializer = [AFHTTPResponseSerializer serializer];
//        [peration setSecurityPolicy:securityPolicy];
        peration.securityPolicy.allowInvalidCertificates=YES;
        
        //
        
        NSString *teststr=[kUserDefaults objectForKey:kAccessToken];
//        params[@"Authorization"] = [kUserDefaults objectForKey:kAccessToken];
        
//
        
        [peration.requestSerializer setValue:teststr forHTTPHeaderField:@"Authorization"];
        [peration.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content_Type"];
//                peration.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
        [peration POST:finalUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
          id st=   operation.responseData;
         NSString *resStr=    operation.responseString;
             NSError *error = nil;
             NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
             
             success(json);
         }
               failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             
             id st=   operation.responseData;
             NSString *resStr=    operation.responseString;
             NSLog(@"resStr===%@",resStr);
             failure(error);
         }];
    }
    else
    {
        //GET方式请求数据
        AFHTTPRequestOperationManager *peration = [AFHTTPRequestOperationManager manager];
        peration.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        
        NSString *teststr=[kUserDefaults objectForKey:kAccessToken];
        //        params[@"Authorization"] = [kUserDefaults objectForKey:kAccessToken];
        
        //
        
        [peration.requestSerializer setValue:teststr forHTTPHeaderField:@"Authorization"];
        //        peration.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//        [peration.requestSerializer setValue:<#(NSString *)#> forHTTPHeaderField:<#(NSString *)#>];
        [peration GET:finalUrl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSError *error = nil;
            NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
            success(json);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            failure(error);
        }];
    }

    
    }

+ (BOOL)www
{
    
    
    __block  BOOL Reach;
    
   
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
       
        
        
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:{
                NSLog(@"网络不通");
                Reach = NO;
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                NSLog(@"网络通过WIFI连" );
                Reach = YES;
                break;
            }
                
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                NSLog(@"网络通过流量连" );
                Reach = YES;
                break;
            }
            default:
                Reach = YES;
                break;
        }
        
    }];
    
    
    return Reach;

}

@end
